package com.demo.dao.impl;

import com.demo.util.Util;
import com.demo.dao.SalaryDAO;
import com.demo.bean.Salary;

import java.io.Serializable;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Salary模块的DAO层（数据层）的具体实现类，对SalaryDAO接口中定义的增删改查等抽象方法作出具体的功能实现
 */
public class SalaryDAOImpl implements SalaryDAO {

    //@Override
    public void add(Salary vo) {
        String sql = "insert into `t_salary` (`salary_no`,`salary_dept`,`salary_name`,`salary_month`,`salary_basic`,`five_insurances`,`accumulation_fund`,`salary_overtime`,`salary_money`,`salary_real`,`salary_text`) values(?,?,?,?,?,?,?,?,?,?,?)";
        try {
            Connection c = Util.getConnection();
            PreparedStatement ps = c.prepareStatement(sql);

            ps.setString(1, vo.getSalaryNo());
            ps.setString(2, vo.getSalaryDept());
            ps.setString(3, vo.getSalaryName());
            ps.setString(4, vo.getSalaryMonth());
            ps.setString(5, vo.getSalaryBasic());
            ps.setString(6, vo.getFiveInsurances());
            ps.setString(7, vo.getAccumulationFund());
            ps.setString(8, vo.getSalaryOvertime());
            ps.setString(9, vo.getSalaryMoney());
            ps.setString(10, vo.getSalaryReal());
            ps.setString(11, vo.getSalaryText());
            ps.execute();
            ps.close();
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //@Override
    public void update(Salary vo) {
        String sql = "update `t_salary` set `salary_no` = ? ,`salary_dept` = ? ,`salary_name` = ? ,`salary_money` = ? ,`salary_text` = ?  where `id` = ?";
        try {
            Connection c = Util.getConnection();
            PreparedStatement ps = c.prepareStatement(sql);

            ps.setString(1, vo.getSalaryNo());
            ps.setString(2, vo.getSalaryDept());
            ps.setString(3, vo.getSalaryName());
            ps.setString(4, vo.getSalaryMoney());
            ps.setString(5, vo.getSalaryText());
            ps.setLong(6, vo.getId());
            ps.execute();
            ps.close();
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //@Override
    public boolean delete(long id) {
        try {
            Connection c = Util.getConnection();
            Statement s = c.createStatement();
            String sql = "delete from `t_salary` where id = " + id;
            s.execute(sql);
            s.close();
            c.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    //@Override
    public Salary get(Serializable id) {
        Salary vo = null;
        try {
            Connection c = Util.getConnection();
            Statement s = c.createStatement();
            String sql = "select * from `t_salary` where id = " + id;
            ResultSet rs = s.executeQuery(sql);
            if (rs.next()) {
                vo = new Salary();
                vo.setId(rs.getLong("id"));
                vo.setSalaryNo(rs.getString("salary_no"));
                vo.setSalaryDept(rs.getString("salary_dept"));
                vo.setSalaryName(rs.getString("salary_name"));
                vo.setSalaryMonth(rs.getString("salary_month"));
                vo.setSalaryBasic(rs.getString("salary_basic"));
                vo.setFiveInsurances(rs.getString("five_insurances"));
                vo.setAccumulationFund(rs.getString("accumulation_fund"));
                vo.setSalaryOvertime(rs.getString("salary_overtime"));
                vo.setSalaryMoney(rs.getString("salary_money"));
                vo.setSalaryReal(rs.getString("salary_real"));
                vo.setSalaryText(rs.getString("salary_text"));
            }
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return vo;
    }

    //@Override
    public Map<String, Object> list(Map<String, Object> params) {
        List<Salary> list = new ArrayList();
        int totalCount = 0;
        String condition = "";
        String sqlList;
        if (params.get("searchColumn") != null && !"".equals(params.get("searchColumn"))) {
            condition += " and `" + params.get("searchColumn") + "` like '%" + params.get("keyword") + "%'";
        }
        try {
            Connection c = Util.getConnection();
            PreparedStatement ps;
            ResultSet rs;
            String limit = (params.get("startIndex") != null && params.get("pageSize") != null) ? " limit " + params.get("startIndex") + "," + params.get("pageSize") : "";
            sqlList = "select * from `t_salary` where 1=1 " + condition + " order by id asc " + limit + ";";
            ps = c.prepareStatement(sqlList);
            rs = ps.executeQuery();
            while (rs.next()) {
                Salary vo = new Salary();
                vo.setId(rs.getLong("id"));
                vo.setSalaryNo(rs.getString("salary_no"));
                vo.setSalaryDept(rs.getString("salary_dept"));
                vo.setSalaryName(rs.getString("salary_name"));
                vo.setSalaryMonth(rs.getString("salary_month"));
                vo.setSalaryBasic(rs.getString("salary_basic"));
                vo.setFiveInsurances(rs.getString("five_insurances"));
                vo.setAccumulationFund(rs.getString("accumulation_fund"));
                vo.setSalaryOvertime(rs.getString("salary_overtime"));
                vo.setSalaryMoney(rs.getString("salary_money"));
                vo.setSalaryReal(rs.getString("salary_real"));
                vo.setSalaryText(rs.getString("salary_text"));
                list.add(vo);
            }
            String sqlCount = "select count(*) from `t_salary` where 1=1 " + condition;
            ps = c.prepareStatement(sqlCount);
            rs = ps.executeQuery();
            if (rs.next()) {
                totalCount = rs.getInt(1);
            }
            rs.close();
            ps.close();
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map<String, Object> result = new HashMap();
        result.put("list", list);
        result.put("totalCount", totalCount);
        return result;
    }
}
